#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<unordered_map>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn = 1e5 + 100;
typedef long long LL;
inline LL read() {
    LL x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * f;
}
int main(void) {
    LL n;
    n = read();
    LL ans = 0;
    bool flag = 1;
    for (LL i = 1; i <= n; i++) {
        LL x, y;
        x = read();
        y = read();
        if (x < y)
            swap(x, y);
        if (x == y)
            continue;
        if (flag == 0)
            continue;
        unordered_map<LL, LL>map1;
        LL res = 0x3f3f3f3f;
        map1[x] = 0;
        LL cnt = 0;
        LL temp = x;
        while (x % 5 == 0) {
            x /= 5;
            cnt++;
            x *= 3;
            if (!map1.count(x))
                map1[x] = cnt;
            else
                map1[x] = min(map1[x], cnt);
        }
        while (x % 3 == 0) {
            x /= 3;
            cnt++;
            if (!map1.count(x))
                map1[x] = cnt;
            else
                map1[x] = min(map1[x], cnt);
        }
        cnt = 0;
        while (temp % 3 == 0) {
            temp /= 3;
            cnt++;
            if (!map1.count(temp))
                map1[temp] = cnt;
            else
                map1[temp] = min(map1[temp], cnt);
        }

        LL o = 0;
        LL t = y;
        LL k = y;
        if (map1.count(t)) {
            res = min(res, map1[t] + o);
        }

        while (t % 5 == 0) {
            t /= 5;
            o++;
            t *= 3;
            if (map1.count(t)) {
                res = min(res, map1[t] + o);
            }
        }
        while (t % 3 == 0) {
            t /= 3;
            o++;
            if (map1.count(t)) {
                res = min(res, map1[t] + o);
            }
        }
        o = 0;
        while (k % 3 == 0) {
            k /= 3;
            o++;
            if (map1.count(k)) {
                res = min(res, map1[k] + o);
            }
        }
        if (res == 0x3f3f3f3f) {
            flag = 0;
            continue;
        } else
            ans += res;
    }
    if (flag == 0)
        printf("-1\n");
    else
        printf("%lld\n", ans);
    return 0;
}
